<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\common\model;


class Goods extends Base{

  protected $autoWriteTimestamp = true;
  protected $auto = ['cat_sort'];
  protected $insert = ['status' => -1];
  protected $update = ['sort'=> 0];

  protected $type       = [
         'jh_ktime' => 'timestamp:Y-m-d h:i',
         'jh_jtime' => 'timestamp:Y-m-d h:i',
         'update_time' => 'timestamp:Y-m-d h:i',
         'create_time' => 'timestamp:Y-m-d h:i',
         'group_time' => 'timestamp:Y-m-d h:i',
  ];
  public function setCatSortAttr($value, $data)
  {
      if($data['cat_id']){
        $sort = db('Goodscat')->where('id',$data['cat_id'])->value('sort');
        return $sort;
      }

  }

  public function goodsrule(){
    return $this->hasMany('Goodsrule');
  }

  public function addfield(){
  $catlist = $this->getGoodscat();
  $shopslist = $this->getGoodshops();

  return array(
    '基础' => array(
        array('name' => 'cat_id', 'title' => '商品所属分类', 'help' => '', 'type' => 'select', 'option' => $catlist, 'help' => '<span style="color:red;">(必填)</span>'),
        array('name' => 'good_name', 'title' => '商品名称', 'help' => '', 'type' => 'text', 'help' => '<span style="color:red;">(必填)</span>'),
        array('name' => 'cover_id', 'title' => '商品封面', 'help' => '', 'type' => 'image', 'help' => '<span style="color:red;">(必填)</span>'),
        //array('name' => 'shop_id', 'title' => '所属商家', 'help' => '', 'type' => 'select', 'option' => $shopslist),
        array('name' => 'good_brand', 'title' => '商品品牌', 'help' => '', 'type' => 'text'),
        array('name' => 'good_brand_dec', 'title' => '商品品牌备注', 'help' => '', 'type' => 'text'),
        array('name' => 'good_desc', 'title' => '特色描述', 'help' => '', 'type' => 'text', 'help' => '<span style="color:red;">(必填)</span>'),
        array('name' => 'rule', 'title' => '规格', 'help' => '格式为 <span style="color:red;">名称:价格:库存:重量</span>', 'type' => 'addlist'),
        array('name' => 'sort', 'title' => '排序', 'help' => '','default'=>'0', 'type' => 'num'),
        array('name'=>'is_hot', 'title'=>'是否推荐','default'=>'0', 'type'=>'radio', 'help'=>'', 'option'=>array('1'=>'推荐','0'=>'不推荐')),
        array('name'=>'status', 'title'=>'状态','default'=>'-1', 'type'=>'radio', 'help'=>'', 'option'=>array('1'=>'上架','-1'=>'下架')),
        array('name'=>'is_timelimit', 'title'=>'是否限时','default'=>'0', 'type'=>'radio', 'help'=>'', 'option'=>array('0'=>'否','1'=>'是')),
        array('name' => 'timelimit', 'title' => '限时时间', 'help' => '', 'type' => 'timelimit'),
        array('name'=>'isshowdelivery', 'title'=>'是否显示特殊配送说明','default'=>'0', 'type'=>'radio', 'help'=>'', 'option'=>array('1'=>'是','0'=>'否')),
        array('name'=>'deliverydec', 'title'=>'特殊配送说明', 'type'=>'text'),
        array('name'=>'is_pinkage', 'title'=>'是否是包邮商品', 'default'=>'0', 'type'=>'radio', 'help'=>'', 'option'=>array('1'=>'是','0'=>'否')),
    ),
    '内容' => array(
      array('name' => 'content', 'title' => '详细描述', 'type' => 'editor', 'help' => ''),
    ),
  );
  }

  public function addsubfield(){
  $catlist = $this->getGoodscat();
  $shopslist = $this->getGoodshops();

  return array(
    '基础' => array(
      array('name' => 'shop_id', 'title' => '所属商家', 'help' => '', 'type' => 'select', 'option' => $shopslist),
      array('name' => 'good_name', 'title' => '标题', 'help' => '', 'type' => 'text'),
      array('name' => 'cover_id', 'title' => '商品封面', 'help' => '', 'type' => 'image'),
      array('name' => 'yd_type', 'title' => '配送或产地直发', 'help' => '', 'type' => 'radio', 'option' => array('0' => '配送','1' => '产地直发')),
      // array('name' => 'yd_sj_time', 'title' => '设置上下架时间', 'help' => '', 'type' => 'yuding'),
      //array('name' => 'yd_fh_time', 'title' => '发货说明', 'help' => '', 'type' => 'text'),

      // array('name' => 'group_time', 'title' => '配送时间', 'help' => '', 'type' => 'datetime'),
      // array('name' => 'group_dec', 'title' => '团购说明', 'help' => '', 'type' => 'text'),
      array('name' => 'sort', 'title' => '排序', 'help' => '', 'type' => 'num'),
      array('name' => 'sendsms_phone', 'title' => '收短信手机号码', 'help' => '', 'type' => 'text'),
        array('name' => 'good_brief', 'title' => '简介', 'help' => '', 'type' => 'text'),
      // array('name' => 'cost_price', 'title' => '成本价格', 'help' => '', 'type' => 'text'),
      // array('name' => 'agent_price', 'title' => '代理价格', 'help' => '', 'type' => 'text'),
      array('name' => 'vs_type', 'title' => 's', 'help' => '', 'type' => 'hidden'),
      array('name' => 'rule', 'title' => '规格', 'help' => '格式为 <span style="color:red;">名称:价格:库存:重量</span>', 'type' => 'addlist')
    ),
    '内容' => array(
      array('name' => 'content', 'title' => '详细说明', 'type' => 'editor', 'help' => ''),
    ),
  );
  }


  public function getGoodscat(){
      $list = db('goodscat')->where('status',1)->field('id,cat_name,goods_type')->order('sort asc, id desc')->select();
      $nlist = array();
      foreach ($list as $key => $value) {
       // $nlist[$value['id']] = $value['cat_name'].'('.parse_config_value($value['goods_type'], 'goods_type').')';
          $nlist[$value['id']] = $value['cat_name'];
      }

      return $nlist;
  }
  public function getGoodshops(){
      $list = db('goodshops')->where('status',1)->column('shopname','id');
      return $list;
  }

  public static function getCountBymap($map){
    $count = self::where($map)->count();
    return $count;
  }

  public static function getPaginateBymap($map, $order='id desc', $page=20){
    $list = self::where($map)->order('sort,update_time desc,id desc')->paginate($page);
    return $list;
  }

}
